﻿Imports Autodesk.AutoCAD.ApplicationServices

Public Class CLDS_Connection_Container
    Private CA As CLDS_ACAD
    Public Property pCA() As CLDS_ACAD
        Get
            pCA = CA
        End Get
        Set(ByVal value As CLDS_ACAD)
            CA = value
        End Set
    End Property
    Public Sub OnApplChanged(ByVal p_appl As Integer)
        Me.txAppl.Text = p_appl
        CA.pProps.v_appl = p_appl
    End Sub
    Public Sub Initialize(ByVal p_CA As CLDS_ACAD)
        CA = p_CA
        If Not CA.pProps Is Nothing Then
            Me.txURL.Text = CA.pProps.v_url
            Me.txUserID.Text = CA.pProps.v_userid
            Me.txPasswd.Text = CA.pProps.v_passwd
            Me.tbEncrypted.Text = CLDS_Static_Utils.CLDS_Encrypt(CA.pProps.v_passwd)
            Me.txPrUserID.Text = CA.pProps.v_pruser
            Me.txPrPasswd.Text = CA.pProps.v_prpass
            Me.txProjid.Text = CA.pProps.v_projid
            Me.txAppl.Text = CA.pProps.v_appl
            Me.cbVFocus.Text = CA.pProps.v_focus
            Me.pgPrefs.SelectedObject = CA.pPrefs
            Me.txURL.ReadOnly = True
            Me.txUserID.ReadOnly = True
            Me.txPrUserID.ReadOnly = True
            Me.txPrPasswd.ReadOnly = True
            Me.txProjid.ReadOnly = True
            Me.txAppl.ReadOnly = True
        End If
        If pCA.pCMAPI.CMUnits = 1 Then
            Me.cbVFocus.Items.Clear()
            Me.cbVFocus.Items.Add("1")
            Me.cbVFocus.Text = "1"
            Me.cbVFocus.Enabled = False
        Else
            Me.cbVFocus.Items.Clear()
            For i As Integer = 1 To pCA.pCMAPI.CMUnits
                Me.cbVFocus.Items.Add(Trim(Str$(i)))
            Next
            Me.cbVFocus.Text = pCA.pProps.v_focus
            Me.cbVFocus.Enabled = True
        End If
    End Sub
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub
    Public Sub PropsToolTip(ByVal v_path As String)
        ttConnect.SetToolTip(gbProject, "Configuration file:" & vbCrLf & v_path)
    End Sub

    Private Sub txPrUserID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CA.pProps.v_pruser = txPrUserID.Text
    End Sub

    Private Sub txPrPasswd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CA.pProps.v_prpass = txPrPasswd.Text
    End Sub

    Private Sub txURL_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txURL.TextChanged
        CA.pProps.v_url = txURL.Text
    End Sub

    Private Sub txUserID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txUserID.TextChanged
        CA.pProps.v_userid = txUserID.Text
    End Sub

    Private Sub txPasswd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txPasswd.TextChanged
        CA.pProps.v_passwd = txPasswd.Text
        tbEncrypted.Text = CLDS_Static_Utils.CLDS_Encrypt(txPasswd.Text)
    End Sub

    Private Sub txProjid_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txProjid.TextChanged
        CA.pProps.v_projid = txProjid.Text
    End Sub

    Private Sub txAppl_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txAppl.TextChanged
        CA.pProps.v_appl = txAppl.Text
    End Sub

    Private Sub btTestConn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTestConn.Click
        CA.pPrefs.LoadPreferences()
        lbConnStatus.Text = "Connecting..."
        lbConnStatus.Update()
        If CA.CLDS_URLConnection(True) Then
            CA.CLDS_GetProjectDefaults()
            If CA.pTokens.Count = 0 Then
                lbConnStatus.Text = "Disconnected"
            Else
                CA.CLDS_RefreshLists()
                CA.CLDS_CMPICPaletteTitle()
                lbConnStatus.Text = "Connected"
            End If
        Else
            lbConnStatus.Text = "Disconnected"
        End If
        lbConnStatus.Update()
    End Sub

    Private Sub btClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClose.Click
        CA.CLDS_DropConnection()
        lbConnStatus.Text = "Disconnected"
        lbConnStatus.Update()
    End Sub

    Private Sub btSavePrefs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSavePrefs.Click
        CA.pPrefs.SavePreferences()
        CA.CLDS_ColourLayerByPrefs()
        Me.btSavePrefs.Enabled = False
    End Sub
    Private Sub pgPrefs_PropertyValueChanged(ByVal s As Object, ByVal e As System.Windows.Forms.PropertyValueChangedEventArgs) Handles pgPrefs.PropertyValueChanged
        Me.btSavePrefs.Enabled = True
    End Sub

    Private Sub cbVFocus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbVFocus.SelectedIndexChanged
        CA.pProps.v_focus = cbVFocus.Text
        CA.CLDS_CMPICPaletteTitle()
    End Sub

    Private Sub btRFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRFind.Click
        CA.CLDS_FindReplace(Me, Me.txRFind.Text, "")
    End Sub

    Private Sub btReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btReplace.Click
        CA.CLDS_FindReplace(Me, Me.txRFind.Text, Me.txReplace.Text)
    End Sub

    Private Sub cxRDevID_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cxRDevID.CheckedChanged
        Dim cx As CheckBox = DirectCast(sender, CheckBox)
        If cx.Checked Then
            Me.cxRNearNode.Enabled = True
        Else
            Me.cxRNearNode.Checked = False
            Me.cxRNearNode.Enabled = False
        End If
    End Sub
End Class
